<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>壁纸下载器</title>
    <style>
        body {
            font-family: 'Microsoft YaHei', Arial, sans-serif;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
            background-color: #f5f5f5;
        }
        h1 {
            color: #333;
            text-align: center;
        }
        .container {
            background-color: white;
            border-radius: 8px;
            padding: 20px;
            box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
        }
        .form-group {
            margin-bottom: 15px;
        }
        label {
            display: block;
            margin-bottom: 5px;
            font-weight: bold;
        }
        input {
            width: 100%;
            padding: 8px;
            border: 1px solid #ddd;
            border-radius: 4px;
            box-sizing: border-box;
        }
        button {
            background-color: #4CAF50;
            color: white;
            border: none;
            padding: 10px 15px;
            border-radius: 4px;
            cursor: pointer;
            font-size: 16px;
        }
        button:hover {
            background-color: #45a049;
        }
        button:disabled {
            background-color: #cccccc;
            cursor: not-allowed;
        }
        #result {
            margin-top: 20px;
            padding: 10px;
            border-left: 4px solid #4CAF50;
            background-color: #f9f9f9;
            display: none;
        }
        .progress {
            margin-top: 10px;
            display: none;
        }
        .progress-bar-container {
            height: 20px;
            background-color: #f1f1f1;
            border-radius: 4px;
            margin-top: 5px;
        }
        .progress-bar {
            height: 20px;
            background-color: #4CAF50;
            width: 0%;
            border-radius: 4px;
            transition: width 0.3s;
        }
        .thumbnails {
            display: flex;
            flex-wrap: wrap;
            gap: 10px;
            margin-top: 15px;
        }
        .thumbnail {
            width: 150px;
            height: 100px;
            object-fit: cover;
            border-radius: 4px;
            box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
            transition: transform 0.2s;
            cursor: pointer;
        }
        .thumbnail:hover {
            transform: scale(1.05);
        }
        .modal {
            display: none;
            position: fixed;
            z-index: 1;
            left: 0;
            top: 0;
            width: 100%;
            height: 100%;
            background-color: rgba(0, 0, 0, 0.7);
        }
        .modal-content {
            margin: auto;
            display: block;
            max-width: 90%;
            max-height: 90%;
            margin-top: 50px;
        }
        .close {
            position: absolute;
            top: 15px;
            right: 35px;
            color: #f1f1f1;
            font-size: 40px;
            font-weight: bold;
            cursor: pointer;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>壁纸下载器</h1>
        <div class="form-group">
            <label for="downloadCount">下载数量:</label>
            <input type="number" id="downloadCount" min="1" max="10" value="1">
        </div>
        <div class="form-group">
            <label for="downloadPath">下载路径:</label>
            <input type="text" id="downloadPath" value="E:\autoImg">
        </div>
        <button id="downloadBtn">开始下载</button>
        
        <div class="progress">
            <p id="progressText">准备下载...</p>
            <div class="progress-bar-container">
                <div class="progress-bar"></div>
            </div>
        </div>
        
        <div id="result"></div>
        
        <div class="thumbnails" id="thumbnails"></div>
    </div>
    
    <!-- 图片预览模态框 -->
    <div id="imageModal" class="modal">
        <span class="close">&times;</span>
        <img class="modal-content" id="modalImage">
    </div>

    <script>
        // 生成唯一会话ID
        function generateSessionId() {
            return 'session_' + Date.now() + '_' + Math.random().toString(36).substr(2, 9);
        }
        
        // 获取图片文件名
        function getFilenameFromPath(path) {
            return path.split('\\').pop();
        }
        
        document.getElementById('downloadBtn').addEventListener('click', function() {
            const count = document.getElementById('downloadCount').value;
            const downloadPath = document.getElementById('downloadPath').value;
            const resultDiv = document.getElementById('result');
            const progressDiv = document.querySelector('.progress');
            const progressBar = document.querySelector('.progress-bar');
            const progressText = document.getElementById('progressText');
            const thumbnailsDiv = document.getElementById('thumbnails');
            const downloadBtn = document.getElementById('downloadBtn');
            
            // 清空之前的结果
            resultDiv.style.display = 'none';
            resultDiv.innerHTML = '';
            thumbnailsDiv.innerHTML = '';
            
            // 显示进度条
            progressDiv.style.display = 'block';
            progressBar.style.width = '0%';
            progressText.textContent = '准备下载...';
            
            // 禁用下载按钮
            downloadBtn.disabled = true;
            
            // 生成会话ID
            const sessionId = generateSessionId();
            
            // 创建SSE连接来接收进度更新
            const eventSource = new EventSource(`/api/wallpaper/progress/${sessionId}`);
            
            eventSource.addEventListener('PROGRESS', function(event) {
                const data = JSON.parse(event.data);
                progressBar.style.width = data.percentage + '%';
                progressText.textContent = data.message;
            });
            
            eventSource.addEventListener('COMPLETE', function(event) {
                const results = JSON.parse(event.data);
                
                // 显示结果
                resultDiv.style.display = 'block';
                let resultHtml = '<h3>下载结果:</h3><ul>';
                
                // 处理每个下载结果
                results.forEach(result => {
                    resultHtml += `<li>${result.message}</li>`;
                    
                    // 如果有图片路径，添加缩略图
                    if (result.imagePath) {
                        const filename = getFilenameFromPath(result.imagePath);
                        const thumbnailUrl = `/images/${filename}`;
                        
                        // 创建缩略图元素
                        const img = document.createElement('img');
                        img.src = thumbnailUrl;
                        img.alt = '壁纸缩略图';
                        img.className = 'thumbnail';
                        img.onclick = function() {
                            document.getElementById('modalImage').src = thumbnailUrl;
                            document.getElementById('imageModal').style.display = 'block';
                        };
                        
                        thumbnailsDiv.appendChild(img);
                    }
                });
                
                resultHtml += '</ul>';
                resultDiv.innerHTML = resultHtml;
                
                // 关闭SSE连接
                eventSource.close();
                
                // 启用下载按钮
                downloadBtn.disabled = false;
            });
            
            eventSource.addEventListener('ERROR', function(event) {
                progressDiv.style.display = 'none';
                resultDiv.style.display = 'block';
                resultDiv.innerHTML = `<p>错误: ${event.data}</p>`;
                
                // 关闭SSE连接
                eventSource.close();
                
                // 启用下载按钮
                downloadBtn.disabled = false;
            });
            
            // 发送异步下载请求
            fetch(`/api/wallpaper/download-async/${count}?sessionId=${sessionId}&downloadPath=${encodeURIComponent(downloadPath)}`, {
                method: 'POST'
            }).catch(error => {
                progressDiv.style.display = 'none';
                resultDiv.style.display = 'block';
                resultDiv.innerHTML = `<p>错误: ${error.message}</p>`;
                
                // 关闭SSE连接
                eventSource.close();
                
                // 启用下载按钮
                downloadBtn.disabled = false;
            });
        });
        
        // 关闭模态框
        document.querySelector('.close').addEventListener('click', function() {
            document.getElementById('imageModal').style.display = 'none';
        });
        
        // 点击模态框外部关闭
        window.addEventListener('click', function(event) {
            const modal = document.getElementById('imageModal');
            if (event.target == modal) {
                modal.style.display = 'none';
            }
        });
    </script>
</body>
</html> 